package b0.controller;


import b0.bean.ResultInfo;
import b0.entity.Comment;
import b0.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    CommentRepository commentRepository;

    @GetMapping("/{id}")
    public ResultInfo getComment(@PathVariable("id") Integer id) {
        ResultInfo resultInfo = new ResultInfo();
        Comment comment = commentRepository.getOne(id);
        resultInfo.setData(comment);
        return resultInfo;
    }

    //增加
    @PostMapping("/add")
    public ResultInfo addComment(Comment comment) {
        ResultInfo resultInfo = new ResultInfo();
        if (comment.getContent() != null){
            Comment save = commentRepository.save(comment);
            resultInfo.setStatus(200);
            resultInfo.setMsg("成功添加");
            resultInfo.setData(save);

        } else {
            resultInfo.setStatus(400);
            resultInfo.setMsg("添加失败");
            resultInfo.setData(comment);
        }
        return resultInfo;
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public ResultInfo deletePoem(@PathVariable("id")Integer id){
        ResultInfo resultInfo =new ResultInfo();
        commentRepository.deleteById(id);
        resultInfo.setStatus(200);
        resultInfo.setMsg("成功删除");
        return resultInfo;
    }


    @PutMapping("/update")
    public ResultInfo updatePoem(Comment comment){
        ResultInfo resultInfo = new ResultInfo();
        if (comment.getContent() != null){
            Comment save = commentRepository.save(comment);
            resultInfo.setStatus(200);
            resultInfo.setMsg("成功更新");
            resultInfo.setData(save);

        } else {
            resultInfo.setStatus(400);
            resultInfo.setMsg("添加失败");
            resultInfo.setData(comment);
        }
        return resultInfo;
    }

}
